#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 30 avr. 2011

@author: alinehuf

UIElement includes classes managing the creation of graphic elements 
of the battle ground and characters
'''
from PyQt4 import QtGui, QtSvg, QtCore

from ui.UiDimension import UiDimension
uiD = UiDimension()

class UiDamier(QtGui.QWidget):
    def __init__(self, parent, x, y, w, h, tailleCase, width, height):
        '''
        Creation of the battleground "damier"
        '''
        super(UiDamier, self).__init__(parent)
        self.setGeometry(x, y, w, h)
        self.image = UiAfficheurSvg(self, \
                     uiD.imagesPath + uiD.imagesTerrains[parent.niveau],0, 0, w, h)
        self.cases = {}
        for x in range(0, width):
            for y in range(0, height):
                nomCase = chr(65 + x) + str(y+1) # A1, A2... B5...
                self.cases[nomCase] = UiCase(self, parent, nomCase,\
                                             x, y, tailleCase)

class UiCase(QtGui.QWidget):
    def __init__(self, parent, mainWindow, nomCase, x, y, tailleCase):
        '''
        Creation of a cell of the battleground "damier"
        '''
        super(UiCase, self).__init__(parent)
        self.name = nomCase
        self.x = x
        self.y = y
        self.mainWindow = mainWindow # la fenetre mère      
        self.setGeometry(x * tailleCase, y * tailleCase, tailleCase, tailleCase)
        self.fond = QtGui.QWidget(self)
        self.fond.resize(tailleCase, tailleCase)
        self.fond.setStyleSheet("QWidget { " + uiD.styleCaseUnSelected + "}") 
    
    def mousePressEvent(self, mouseEvent):
        '''
        Detect a click on a cell an inform the 'buisiness' of it
        '''
        self.mainWindow.controleur.mousePressEvent( \
                                                self.name, mouseEvent.button() )
    
    def setStyle(self, style):
        '''
        Change the visual status of the cell
        '''
        self.fond.setStyleSheet("QWidget { " + style + " }")

class UiAfficheurSvg(QtGui.QWidget):
    def __init__(self, parent, fichier, x, y, w, h):
        '''
        create a canvas an render the SVG file onto it
        '''
        super(UiAfficheurSvg, self).__init__(parent)
        self.fichier = fichier
        self.setGeometry(x, y, w, h)
    
    def paintEvent(self, e):
        self.toile = QtGui.QPainter()
        self.toile.begin(self)
        self.image = QtSvg.QSvgRenderer(self.fichier,self)
        self.image.render(self.toile)
        self.toile.end()

class UiPersonnage(QtGui.QWidget):
    def __init__(self, damier, personnage, tailleCase):
        '''
        Create a Character with a set of differents aspects 
        (turn left, turn right, dead, ...) 
        '''
        super(UiPersonnage, self).__init__(damier)
        self.nom = personnage
        self.aspect = ""
        self.x = 0
        self.y = 0
        w = uiD.imagesPersonagesWidth * tailleCase
        h = uiD.imagesPersonagesHeight * tailleCase
        self.images = {}
        for image in uiD.imagesPersonages[personnage] :
            self.images[image] = UiAfficheurSvg(self, uiD.imagesPath + \
                            uiD.imagesPersonages[personnage][image], 0, 0, w, h)
            self.images[image].setVisible(False)
        self.setGeometry(self.x, self.y, w, h)
        self.setVisible(True)
        self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents, True)
    
    def meurt(self):
        self._changeAspect("dead")
    
    def ressucite(self):
        self._changeAspect(self.aspect)
    
    def regardeGauche(self):
        self.aspect = "left"
        self._changeAspect("left")
        
    def regardeDroite(self):
        self.aspect = "right"
        self._changeAspect("right")
        
    def _changeAspect(self, aspect):
        '''
        Set the image corresponding to the current aspect to visible
        '''
        for image in self.images :
            self.images[image].setVisible(False)
        self.images[aspect].setVisible(True)
